﻿//Feed event handlers
function FeedEventHandler()
{
	var splitterEnabled = false, splitterPrevPosX, splitterMinPosX, splitterMaxPosX;
	var whereUsed = SSVC.Utils.isNotNullOrUndefined(top.instance) ? "ItemWindow" : "MainWindow";

	var feedObj = null;

	function createFeed()
	{
		return (whereUsed == "ItemWindow") ? new SSVC.Feed.DiscussionFeed() : new SSVC.Feed.MyFeed();
	}

	return {
		onStartPageLoad: function()
		{
			top.aras.showStatusMessage("status", "", "../images/Progress.gif");
		},

		onEndPageLoad: function()
		{
			top.aras.showStatusMessage("status", "Ready", "");
		},

		taReplyMessage_onKeyDown: function(event)
		{
			if (event.keyCode == 13)
			{
				if (event.shiftKey)
					return;

				this.FeedObj.onSendReplyEventHandler(true);
			}
		},

		taNewMessage_onKeyDown: function(event)
		{
			if (event.keyCode == 13)
			{
				if (event.shiftKey)
					return;

				//this.FeedObj.onSendCommentEventHandler(false);
			}
		},

		onClassificationFilterMouseDown: function()
		{
			var item = top.aras.newIOMItem("Secure Message");
			var aml = "<AML><Item type=\"ItemType\"  action=\"get\" select=\"*\"><keyed_name>Secure Message</keyed_name></Item></AML>";
			item.loadAML(aml);
			item = item.apply();

			if (item.isError())
			{
				return;
			}

			var itName = top.aras.getItemProperty(item.node, "name");
			var itID = item.getAttribute("id");
			if (!itName)
			{
				top.aras.AlertError(top.aras.getResource("", "classstructure.itemname_cannot_be_empty"), "", "", window);
				return;
			}

			var class_structure = top.aras.getItemProperty(item.node, "class_structure");

			var classificationDom = top.aras.createXMLDocument();
			if (class_structure === "")
			{
				classificationDom.loadXML("<class id='" + itID + "'/>");
			}
			else
			{
				classificationDom.loadXML(class_structure);
			}

			class_structure = classificationDom.xml;

			var param = {};
			param.aras = top.aras;
			param.dialogType = "classification";
			param.isEditMode = false;
			param.itemTypeName = "Secure Message";
			param.title = top.aras.getResource("", "imports_core.class_structure_for", itName);
			param.class_structure = class_structure;
			param.item = item.node;
			param.selectLeafOnly = true;
			param.isRootClassSelectForbidden = true;

			var res = showModalDialog("../../../scripts/ClassStructureDialog.html", param, "dialogHeight:400px;dialogWidth:300px;help:0;resizable:1;status:0;center:yes;");

			var filter = [];
			if (res === "")
			{
				filter = undefined;
			}
			else if (res)
			{
				var obj = {};
				obj.name = "classification";
				obj.text = res;
				obj.condition = "like";
				filter.push(obj);
			}
			else
			{
				return;
			}

			tbClassificationFilter.innerText = res;
			this.FeedObj = createFeed();
			this.FeedObj.load(filter);
			this.FeedObj.update();
		},

		onBodyMouseDownEventHandler: function()
		{

		},

		onBodyUnloadEventHandler: function()
		{
			//top.aras.unregisterEventHandler("ItemSave", window, this.onItemSave);

		},
		onItemSave: function(params)
		{
			var itemTypeName = params.itemNd.getAttribute("type");
			if (itemTypeName == "Secure Message")
			{
				SSVC.FeedEventHandler.FeedObj.update();
			}
		},

		onBodyMouseMoveEventHandler: function()
		{
			if (splitterEnabled)
			{
				if ((event.x < splitterMinPosX) || (event.x > splitterMaxPosX)) return;
				var deltaX = event.x - splitterPrevPosX;
				splitterPrevPosX = event.x;
				feedContainer.style.width = feedContainer.clientWidth + deltaX + "px";
				if (deltaX > 0)
				{
					snapshotContainer.style.width = snapshotTopContainer.clientWidth - deltaX + "px";
				} else
				{
					snapshotContainer.style.width = snapshotTopContainer.clientWidth + "px";
				}
			}
		},

		onBodyMouseUpEventHanlder: function()
		{
			splitterEnabled = false;
			splitterPrevPosX = 0;
		},

		onBodySelectStartEventHandler: function()
		{
			return !splitterEnabled;
		},

		ShowMoreMessages_OnClick: function()
		{
			SSVC.FeedEventHandler.FeedObj.addMessagesToFeed(false);
		},

		MessageContainer_OnScroll: function(el)
		{
			SSVC.Wrap.Dojo.Require(["dojo/dom-geometry"], function(domGeom)
			{
				var elPosition = domGeom.position(el);
				if (elPosition.y >= elPosition.h)
					this.FeedObj.addMessagesToFeed(false);

			});
		}
	};
}

SSVC.Wrap.Dojo.SetObject("SSVC.FeedEventHandler", new FeedEventHandler());